//
//  VideoHeaderDetailViewController.m
//  Project_B
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "VideoHeaderDetailViewController.h"
#import "VideoHeaderDetailCollectionViewCell.h"
#import "LJDTools.h"
#import "VideoHeaderDetailModel.h"

@interface VideoHeaderDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *allArray;
// 页码
@property (nonatomic, assign)NSInteger page;
// 判断上拉还是下拉
@property (nonatomic, assign)BOOL isUpDown;

@end

@implementation VideoHeaderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 设置导航栏不透明
    self.navigationController.navigationBar.translucent = NO;
    
    
#pragma mark - 创建collectionView
    [self createCollectionView];
    
    
    
#pragma mark - 上拉刷新
    // 设置初始页为0
    self.page = 0;
    // 下拉刷新
    [self headerRefresh];
    // 进界面就开始刷新一次
    [self.collectionView.mj_header beginRefreshing];
    
    
#pragma mark - 下拉刷新
    [self footerRefresh];
    

    
#pragma mark - 初始化数组
    self.allArray = [NSMutableArray array];
    
    
    
    
}



#pragma mark - 下拉刷新
- (void)headerRefresh
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 删除数组（下拉一次删除一次）
        [self.allArray removeAllObjects];
        // 从第一页开始
        self.page = 0;
        // 判断是上拉还是下拉
        self.isUpDown = YES;
        // 请求数据
        [self getData];
        // 刷新界面
        [self.collectionView reloadData];
        // 结束刷新
        [self.collectionView.mj_header endRefreshing];
    }];
    

}



#pragma mark - 上拉加载
- (void)footerRefresh
{
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 每上拉一次，刷新下一页数据（因为数据中10个为一组，所以+10）
        self.page += 10;
        // 判断是上拉还是下拉
        self.isUpDown = NO;
        // 请求数据
        [self getData];
        // 刷新界面
        [self.collectionView reloadData];
        // 结束刷新
        [self.collectionView.mj_footer endRefreshing];
    }];
    
}




#pragma mark - 创建collectionView
- (void)createCollectionView
{
    // 布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // item尺寸
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH - 10, 330);
    // 距离四周距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
    
    // collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    // masonry
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.mas_equalTo(- 49);
        
    }];
    
    
    // 注册cell
    [self.collectionView registerClass:[VideoHeaderDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}




#pragma mark - 请求数据
- (void)getData
{
    [LJDTools getUrl:[NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/%@/n/%ld-10.html", self.sid, self.page] body:nil result:LJDJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:self.sid];
        for (NSMutableDictionary *dic in array) {
            // 赋值
            VideoHeaderDetailModel *headerDetailModel = [VideoHeaderDetailModel modelWithDictionary:dic];
            [self.allArray addObject:headerDetailModel];
        }
        // 刷新
        [self.collectionView reloadData];
        
        
    } failure:^(NSError *error) {
        
        /* 请求失败弹出提示框 */
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络环境较差,请稍后重试" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:alertAction];
        
    }];
}





#pragma mark - item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allArray.count;
    
}





#pragma mark - cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"cell";
    VideoHeaderDetailCollectionViewCell *videoHeaderDetailCell = [collectionView dequeueReusableCellWithReuseIdentifier:Identifier forIndexPath:indexPath];
    videoHeaderDetailCell.backgroundColor = [UIColor whiteColor];
    // 赋值
    videoHeaderDetailCell.headerDetailModel = [self.allArray objectAtIndex:indexPath.row];
    return videoHeaderDetailCell;
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
